package cn.ffcs.unm.staff.action;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Components;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.ListModelList;

import cn.ffcs.raptornuke.plugin.common.zk.ctrl.BasePortletComposer;
import cn.ffcs.raptornuke.plugin.common.zk.util.ZkUtil;
import cn.ffcs.unm.staff.action.bean.SalaryTableMainBean;
import cn.ffcs.unm.staff.manager.StaffNeimBaseInfoManager;
import cn.ffcs.unm.staff.model.SalaryTable;
import cn.ffcs.uom.common.util.DateUtil;
import cn.ffcs.uom.common.util.ListboxUtils;
import cn.ffcs.uom.common.util.NumericUtil;
import cn.ffcs.uom.common.vo.PageInfo;
import cn.ffcs.uom.common.zul.PubUtil;

@Controller
@Scope("prototype")
public class SalaryTableMainComposer extends BasePortletComposer {

	private static final long serialVersionUID = 1664867318498583078L;

	private SalaryTableMainBean bean = new SalaryTableMainBean();

	private SalaryTable qrySalaryTable;

	@SuppressWarnings("unused")
	private SalaryTable salaryTable;

	@Autowired
	@Qualifier("staffNeimBaseInfoManager")
	private StaffNeimBaseInfoManager staffNeimBaseInfoManager;

	@Override
	public void doAfterCompose(Component comp) throws Exception {
		super.doAfterCompose(comp);
		Components.wireVariables(comp, bean);
	}

	/**
	 * 初始化
	 * 
	 * @throws Exception
	 */
	public void onCreate$salaryTableMainWin() throws Exception {
	}

	public void onQuery() {
		clearVariable();
		SalaryTable objSalaryTable = SalaryTable.newInstance();
		PubUtil.fillPoFromBean(bean, objSalaryTable);
		Date monthIdDate = bean.getMonthId().getValue();
		if (monthIdDate == null) {
			ZkUtil.showError("请输入账期", "温馨提示");
			return;
		}
		int index = bean.getRadiogroup().getSelectedIndex();
		String monthId = DateUtil.getYYYYMMDD(monthIdDate);
		if (index == 0) {
			String monthIdMonth = monthId.substring(0, 6);
			objSalaryTable.setMonthId(monthIdMonth);
		}else {
			objSalaryTable.setMonthId(monthId);
		}
		qrySalaryTable = objSalaryTable;
		this.bean.getSalaryTableListboxPaging().setActivePage(0);
		onSalaryTableQuery();
	}

	public void onReset() {
		bean.getCYear().setValue(null);
		clearVariable();
		ListboxUtils.clearListbox(this.bean.getSalaryTableListbox());
		this.bean.getSalaryTableListboxPaging().setTotalSize(1);
	}

	public void onSalaryTableQuery() {
		if (this.qrySalaryTable != null) {
			int activePage = bean.getSalaryTableListboxPaging().getActivePage();
			int pageSize = bean.getSalaryTableListboxPaging().getPageSize();
			PageInfo pageInfo = staffNeimBaseInfoManager
					.queryPageInfoBySalaryTable(qrySalaryTable, activePage + 1,
							pageSize);
			ListModelList dataList = new BindingListModelList(
					pageInfo.getDataList(), true);
			bean.getSalaryTableListbox().setModel(dataList);
			bean.getSalaryTableListboxPaging().setTotalSize(
					NumericUtil.nullToZero(pageInfo.getTotalCount()));
		}
	}

	/**
	 * 清除变量
	 */
	public void clearVariable() {
		qrySalaryTable = null;
		salaryTable = null;
	}

	/**
	 * 翻页
	 */
	public void onListboxPaging() {
		this.onSalaryTableQuery();
	}
}
